﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace EMath.Controls.Arithmetic {
	/// <summary>
	/// Interaction logic for Divisibility.xaml
	/// </summary>
	public partial class Divisibility : UserControl {
		private BackgroundWorker BW;
		private List<int> LpList;
		private double N1 = 0;
		private double N2 = 0;

		public Divisibility( ) {
			InitializeComponent( );
			this.BW = new BackgroundWorker( );
			this.BW.WorkerSupportsCancellation = true;
			this.BW.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BW_RunWorkerCompleted);
			this.BW.DoWork += new DoWorkEventHandler(NWD);
			this.BW.DoWork += new DoWorkEventHandler(NWW);
			this.LpList = new List<int>( );
		}

		public void Clear( ) {
			this.TB1_Num.Text = "";
			this.TB2_Num.Text = "";
			this.TB_NWD.Text = "0";
			this.TB_NWW.Text = "0";
		}

		private void Back(object sender, RoutedEventArgs e) {
			this.Stop(sender, e);
			if (this.Parent is UIElement)
				(this.Parent as UIElement).Visibility = System.Windows.Visibility.Hidden;
		}

		private void Stop(object sender, RoutedEventArgs e) {
			if (this.BW.IsBusy)
				this.BW.CancelAsync( );
		}

		private void TB1_Num_TextChanged(object sender, TextChangedEventArgs e) {
			if (Double.TryParse(this.TB1_Num.Text, out this.N1)) {
				this.TB1_Num.IsEnabled = false;
				this.TB2_Num.IsEnabled = false;
				if (!this.BW.IsBusy)
					this.BW.RunWorkerAsync( );
			}
		}

		private void TB2_Num_TextChanged(object sender, TextChangedEventArgs e) {
			if (Double.TryParse(this.TB2_Num.Text, out this.N2)) {
				this.TB1_Num.IsEnabled = false;
				this.TB2_Num.IsEnabled = false;
				if (!this.BW.IsBusy)
					this.BW.RunWorkerAsync( );
			}
		}

		private void BW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
			this.TB1_Num.IsEnabled = true;
			this.TB2_Num.IsEnabled = true;
		}

		#region Lib
		public delegate void ActionE(double N);

		public void NWD(object sender, DoWorkEventArgs e) {
			BackgroundWorker bw = null;
			if (sender is BackgroundWorker)
				bw = (sender as BackgroundWorker);
			long a = Convert.ToInt64(N1), b = Convert.ToInt64(N2);
			while (a != b && !(a <= 0 || b <= 0)) {
				if (a > b) {
					a -= b;
				} else {
					b -= a;
				}
				if (e.Cancel == true || bw.CancellationPending == true) {
					e.Cancel = true;
					break;
				}
			}
			if (e.Cancel != true)
				Dispatcher.Invoke(
					new ActionE((double N) => {
						this.TB_NWD.Text = N.ToString( );
					}), a
				);

		}
		public void NWW(object sender, DoWorkEventArgs e) {
			BackgroundWorker bw = null;
			if (sender is BackgroundWorker)
				bw = (sender as BackgroundWorker);
			double a = N1, b = N2;
			while (a != b && !(a <= 0 || b <= 0) && !(a > N1 * N2 || b > N1 * N2)) {
				if (a < b) {
					a += N1;
				} else {
					b += N2;
				}
				if (e.Cancel == true || bw.CancellationPending == true) {
					e.Cancel = true;
					break;
				}
			}
			if (e.Cancel != true)
				Dispatcher.Invoke(
					new ActionE((double N) => {
						this.TB_NWW.Text = N.ToString( );
					}), a
				);
		}
		public void Lp(object sender, DoWorkEventArgs e) {
		}
		#endregion
	}
}
